import 'package:flutter_chat/common/utils/utils.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

class LoadingUtil {
  static void loading({
    String text = "加载中...",
  }) {
    EasyLoading.show(
      status: text,
      dismissOnTap: false,
      maskType: EasyLoadingMaskType.black,
    );
  }

  static Future<void> loadingTryAsync({
    String text = "加载中...",
    Function? callback,
    autoClose = true,
  }) async {
    EasyLoading.show(
      status: text,
      dismissOnTap: false,
      maskType: EasyLoadingMaskType.black,
    );
    try {
      await callback?.call();
      if (autoClose) {
        close();
      }
    } catch (e) {
      LogUtil.p("发生错误：$e");
      if (autoClose) {
        close();
      }
    }
  }

  static void close() {
    EasyLoading.dismiss();
  }

  static void success({
    String text = "加载成功",
  }) {
    EasyLoading.showSuccess(text);
  }

  static void error({
    String text = "加载失败",
  }) {
    EasyLoading.showError(text);
  }

  static void info({
    String text = "加载失败",
  }) {
    EasyLoading.showInfo(text);
  }

  static void toast({
    String text = "加载失败",
  }) {
    EasyLoading.showToast(text);
  }
}
